import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'

export const Layout = () => import('@/layout/index.vue')

// 静态路由
export const constantRoutes: RouteRecordRaw[] = [
	{
		path: '/redirect',
		component: Layout,
		meta: { hidden: true },
		children: [
			{
				path: '/redirect/:path(.*)',
				component: () => import('@/views/redirect/index.vue')
			}
		]
	},
	{
		path: '/login',
		component: () => import('@/views/login/index.vue'),
		meta: { hidden: true }
	},
	{
		path: '/',
		name: '/',
		component: Layout,
		redirect: '/dashboard',
		children: [
			{
				path: 'dashboard',
				component: () => import('@/views/dashboard/index.vue'),
				name: 'Dashboard', // 用于 keep-alive, 必须与SFC自动推导或者显示声明的组件name一致
				// https://cn.vuejs.org/guide/built-ins/keep-alive.html#include-exclude
				meta: {
					title: 'dashboard',
					icon: 'homepage',
					affix: true,
					keepAlive: true,
					alwaysShow: false
				}
			},
			{
				path: '401',
				component: () => import('@/views/error-page/401.vue'),
				meta: { hidden: true }
			},
			{
				path: '404',
				component: () => import('@/views/error-page/404.vue'),
				meta: { hidden: true }
			}
		]
	},

	// 外部链接
	// {
	//   path: "/external-link",
	//   component: Layout,
	//   children: [ {
	//       component: () => import("@/views/external-link/index.vue"),
	//       path: "https://www.cnblogs.com/haoxianrui/",
	//       meta: { title: "外部链接", icon: "link" },
	//     },
	//   ],
	// },
	// 多级嵌套路由
	{
		path: '/Generator',
		component: Layout,
		name: 'Generator',
		meta: { title: '生成工具', icon: 'homepage' },
		children: [
			{
				path: '/dbManage',
				name: 'dbManage',
				meta: { title: '库表管理' },
				component: () => import('@/views/generator/index.vue')
			},
			{
				path: '/modelManage',
				name: 'modelManage',
				meta: { title: '模型管理' },
				component: () => import('@/views/generator/index.vue')
			},
			{
				path: '/javaGenerator',
				name: 'javaGenerator',
				meta: { title: 'java生成器' },
				component: () => import('@/views/generator/index.vue')
			},
			{
				path: '/vueGenerator',
				name: 'vueGenerator',
				meta: { title: 'vue生成器' },
				component: () => import('@/views/generator/vueGenerator.vue')
			}
		]
	},
	{
		path: '/',
		component: Layout,
		name: 'Auxiliary',
		meta: { title: '辅助工具', icon: 'homepage' },
		children: [
			{
				path: '/gitManage',
				name: 'gitManage',
				meta: { title: 'git仓库管理' },
				component: () => import('@/views/generator/index.vue')
			},
			{
				path: '/vueComponents',
				name: 'vueComponents',
				meta: { title: 'vue组件维护' },
				component: () => import('@/views/generator/vueComponents.vue')
			},
			{
				path: '/markdown',
				name: 'Markdown',
				meta: { title: 'vue组件md' },
				component: () => import('@/views/generator/markdown.vue')
			}
		]
	}
]

/**
 * 创建路由
 */
const router = createRouter({
	history: createWebHashHistory(),
	routes: constantRoutes,
	// 刷新时，滚动条位置还原
	scrollBehavior: () => ({ left: 0, top: 0 })
})

/**
 * 重置路由
 */
export function resetRouter() {
	router.replace({ path: '/login' })
}

export default router
